#include<stdio.h>
#include<stdlib.h>
#include<string.h>
float *a[100]={NULL};
char *listdb[100];
int size[100]={0};
int i=0;
void insertx(int x,int k,float w)
{   
    if(x>size[k]){printf("\nEnter in the scope please!!"); return;}
    a[k]=(float *)realloc(a[k],(size[k]+1)*sizeof(float));
    if(a[k]==NULL)
    {printf("\nThe End!"); return;}
    int o;
    for(o=size[k];o>=x;o--){
                           a[k][o]=a[k][o-1];}
    a[k][x]=w;
    size[k]++;
    }
void extend(float b[],int k,int x)
{
     int o;
     for(o=0;o<k;o++)
                     insertx(size[x],x,b[o]);
}    
void newl(float b[],int k)
{
     a[i]=(float *)malloc(k*sizeof(float));
     int o;
     for(o=0;o<k;o++)
                     a[i][o]=b[o];
     
     size[i]=k;
     i++;
}
void pop(int o,int k){
     if(o<0||o>=size[k]){printf("\nOut of scope!"); return;}
     
     for(;o<size[k];o++)
                            a[k][o]=a[k][o+1];
     a[k]=(float *)realloc(a[k],(size[k]-1)*sizeof(float));
     size[k]--;}

void del(int k, float y)
{
     int o,flag=0;
     for(o=0;o<size[k];o++)
                           if(a[k][o]==y)
                           {             flag=1;
                                         break;
                                         
                                         }
     if(flag==0)
     {
                printf("\nNot in the list!!!");
                return;}
     pop(o,k);}
void print(int x)
{
     int o;printf("\n%s=",listdb[x]);
     for(o=0;o<size[x];o++)
                           printf("%0.1f,",a[x][o]);
}
int dec(char ch[])
{
     int o;
     for(o=0;o<100;o++)
                       if(strcmp(ch,listdb[o])==0)
                                                  return o;
}
void count(int x,float k)
{
     int o,sum=0;
     for(o=0;o<size[x];o++)
                           if(k==a[x][o])
                                         sum++;
     printf("\n%d times!!",sum);
}
void indx(int x,float k)
{
      int o;
     for(o=0;o<size[x];o++)
                           if(k==a[x][o])
                           {printf("\n%d position!!",o);
                            return;}
     printf("\nNot found!!");
}
void sort(int x)
{    int o,j;
     float Temp;
     for(o=1; o<size[x]; o++)
	{
		Temp = a[x][o];
		j = o-1;
		while(Temp<a[x][j] && j>=0)
		{
			a[x][j+1] = a[x][j];
			j = j-1;
		}
		a[x][j+1] = Temp;
	}                                       
      printf("\nSorted list:");
      print(x);             
      }
 void rev(int x)
 {    float tmp;
      int o=0,k=size[x]-1;
      for(;k>=o;o++,k--)
      {                 tmp=a[x][o];
                        a[x][o]=a[x][k];
                        a[x][k]=tmp;
                        }
                        printf("\nReversed list!");
                        print(x);
                        }
void databprint()
{
	int o;
	for(o=0;o<i;o++)
		print(o);
	printf("\n");
}                               
   
int main()
{
    int choice;
    char g[20];
    do{  r1:
        printf("\nEnter:\n1.Start a New list:\n2.print\n3.insert at x.\n4.Extend.\n5.Remove\n6.Append\n7.POP\n8.Index\n9.Count\n10.sort\n11.Reverse\n12.EXIT");
        scanf("%d",&choice);
        switch(choice){
        case 1:{int j=0,k=0;
              int i2=0;
              char a[100],c[20],b1[100];
           printf("\nEnter the name:");
           scanf("%s",b1);
	   listdb[i]=(char *)calloc(strlen(b1),sizeof(char));
	   strcpy(listdb[i],b1);
           printf("\nEnter list seperated by comma:");
           scanf("%s",a);
           float b[20]={0};
           for(i2=0;a[i2]!='\0';i2++)
           {               if(a[i2]!=',')
                           {     c[j++]=a[i2];                                 
                           }
                           else
                           {   c[j]='\0';
                               b[k++]=atof(c);
                               j=0;
                               strcpy(c,"0");                                                                
                               }
                               }
           b[k++]=atof(c);
           newl(b,k);
           }
           break;
           case 2:{printf("\nEnter name:");
                   scanf("%s",g);
                   
                   print(dec(g));
		   goto r1;}
			
                   break;                       
           case 3:{printf("\nEnter name:");
                   scanf("%s",g);
                   float y;
                   printf("\nElement:");
                   scanf("%f",&y);
                   int k;
                   printf("\nPosition:");
                   scanf("%d",&k);
                   insertx(k,dec(g),y);
                   }break;
           case 4:{int i12;
                    printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\n1.existing list.\n2.New list");
                    scanf("%d",&i12);
                    if(i12==1){
                              char t[20];
                              printf("\nEnter name:");
                              scanf("%s",t);
                              extend(a[dec(t)],size[dec(t)],dec(g));}
                    else{
                              
                    int j=0,k=0;
                    int i2=0;
                    char a[100],c[20];
                    printf("\nEnter list seperated by comma:");
                    scanf("%s",a);
                    float b[20]={0};
                    for(i2=0;a[i2]!='\0';i2++)
                    {               if(a[i2]!=',')
                                           {     c[j++]=a[i2];                                 
                                           }
                                           else
                                           {   c[j]='\0';
                                            b[k++]=atof(c);
                                                j=0;
                                                   strcpy(c,"0");                                                                
                                                      }
                                                      }
                    b[k++]=atof(c);
                    extend(b,k,dec(g));}  }    break;                
           case 5:{printf("\nEnter name:");
                   scanf("%s",g);
                   float y;
                   printf("\nElement:");
                   scanf("%f",&y);
                   del(dec(g),y);
                   }break;                    
           case 6:{printf("\nEnter name:");
                   scanf("%s",g);
                   float y;
                   printf("\nElement:");
                   scanf("%f",&y);
                   insertx(size[dec(g)],dec(g),y);
                   }break;              
           case 7:{printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\nEnter the position(press enter to skip):");
                    
                    char h[4]={0},ch;
                    int i4=0;
                    int t;
                    printf("\nenter");
                    setbuf(stdin,NULL);
                    while ((ch = getchar()) != '\n')
                    {                         h[i4]=ch;
                                     i4++;
                                       }h[i4]='\0';
                    if(i4>0)t=atoi(h);
                    else
                        t=size[dec(g)]-1;  
                    pop(t,dec(g));}break;
           case 8:{printf("\nEnter name:");
                   scanf("%s",g);
                   float y;
                   printf("\nElement:");
                   scanf("%f",&y);
                   indx(dec(g),y);}break;
           case 9:{printf("\nEnter name:");
                   scanf("%s",g);
                   float y;
                   printf("\nElement:");
                   scanf("%f",&y);
                   count(dec(g),y);}break;
           case 10:{printf("\nEnter name:");
                   scanf("%s",g);
                   sort(dec(g));
		goto r1;}break;
           case 11:{printf("\nEnter name:");
                   scanf("%s",g);
                   rev(dec(g));
		   goto r1;}break;       
                 
             }printf("\n///////////////////DATABASE\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
		databprint();}while(choice!=12);
           int z;
           for(z=0;z<100;z++)
                             free(a[z]);
           }
           
        
    


